import request from '@/utils/request'
import type { ApiResponse, Article, ArticleQueryParams, PageResponse } from '@/types/api'

// 文章管理API
export const articleApi = {
  // 分页查询文章列表
  getArticlePage(params: ArticleQueryParams): Promise<ApiResponse<PageResponse<Article>>> {
    return request({
      url: '/admin/article/page',
      method: 'get',
      params,
    })
  },

  // 获取文章详情
  getArticle(id: number | string): Promise<ApiResponse<Article>> {
    return request({
      url: `/admin/article/${id}`,
      method: 'get',
    })
  },

  // 创建文章
  createArticle(data: Partial<Article>): Promise<ApiResponse<void>> {
    return request({
      url: '/admin/article',
      method: 'post',
      data,
    })
  },

  // 更新文章 - 支持字符串ID避免精度丢失
  updateArticle(id: number | string, data: Partial<Article>): Promise<ApiResponse<void>> {
    return request({
      url: `/admin/article/${id}`,
      method: 'put',
      data,
    })
  },

  // 删除文章 - 支持字符串ID避免精度丢失
  deleteArticle(id: number | string): Promise<ApiResponse<void>> {
    return request({
      url: `/admin/article/${id}`,
      method: 'delete',
    })
  },

  // 批量删除文章
  batchDeleteArticles(ids: (number | string)[]): Promise<ApiResponse<void>> {
    return request({
      url: '/admin/article/batch',
      method: 'delete',
      data: ids,
    })
  },

  // 批量发布文章
  batchPublishArticles(ids: (number | string)[]): Promise<ApiResponse<void>> {
    return request({
      url: '/admin/article/batch/publish',
      method: 'put',
      data: ids,
    })
  },

  // 更新文章状态 - 支持字符串ID避免精度丢失
  updateArticleStatus(id: number | string, status: number): Promise<ApiResponse<void>> {
    return request({
      url: `/admin/article/${id}/status`,
      method: 'put',
      params: { status },
    })
  },

  // 发布文章 - 支持字符串ID避免精度丢失
  publishArticle(id: number | string): Promise<ApiResponse<void>> {
    return request({
      url: `/admin/article/${id}/publish`,
      method: 'put',
    })
  },

  // 撤回文章 - 支持字符串ID避免精度丢失
  withdrawArticle(id: number | string): Promise<ApiResponse<void>> {
    return request({
      url: `/admin/article/${id}/withdraw`,
      method: 'put',
    })
  },

  // 获取栏目文章
  getArticlesByChannel(channelId: number, limit = 10): Promise<ApiResponse<Article[]>> {
    return request({
      url: `/admin/article/channel/${channelId}`,
      method: 'get',
      params: { limit },
    })
  },

  // 获取推荐文章
  getRecommendArticles(siteId: number, limit = 10): Promise<ApiResponse<Article[]>> {
    return request({
      url: '/admin/article/recommend',
      method: 'get',
      params: { siteId, limit },
    })
  },

  // 获取热门文章
  getHotArticles(siteId: number, limit = 10): Promise<ApiResponse<Article[]>> {
    return request({
      url: '/admin/article/hot',
      method: 'get',
      params: { siteId, limit },
    })
  },

  // 获取最新文章
  getLatestArticles(siteId: number, limit = 10): Promise<ApiResponse<Article[]>> {
    return request({
      url: '/admin/article/latest',
      method: 'get',
      params: { siteId, limit },
    })
  },

  // 统计栏目文章数量
  countByChannelId(channelId: number): Promise<ApiResponse<number>> {
    return request({
      url: `/admin/article/count/channel/${channelId}`,
      method: 'get',
    })
  },

  // 统计站点文章数量
  countBySiteId(siteId: number): Promise<ApiResponse<number>> {
    return request({
      url: `/admin/article/count/site/${siteId}`,
      method: 'get',
    })
  },

  // 调试文章信息
  debugArticle(id: number): Promise<ApiResponse<any>> {
    return request({
      url: `/admin/article/${id}/debug`,
      method: 'get',
    })
  },
}
